/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-26
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.MemberFavorites;
import com.rzico.basics.entity.Product;
import com.rzico.basics.model.ProductVo;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberFavoritesService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收藏控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-26
 */
@Api(description = "收藏接口")
@RestController
@RequestMapping("/admin/memberFavorites")
public class MemberFavoritesAdminController extends BaseController {

    @Autowired
    private MemberFavoritesService memberFavoritesService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询收藏
     *
     * @return
     */
    @ApiOperation("分页查询收藏")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "Long", paramType = "query")
    })
    public CommResult<ProductVo> list(Long memberId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (memberId!=null) {
            params.put("memberId",memberId);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProductVo> list = memberFavoritesService.selectProductVoList(params);
        PageResult<ProductVo> pageResult = new PageResult<ProductVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 获取收藏商品数量
     *
     * @return
     */
    @ApiOperation("获取收藏商品数量")
    @GetMapping("/count")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "Long", paramType = "query")
    })
    public CommResult<Long> count(Long memberId) {
        Map<String,Object> params = new HashMap<>();
        if (memberId!=null) {
            params.put("memberId",memberId);
        }
        //获取收藏
        Integer fav = memberFavoritesService.selectRowCount(params);
        return CommResult.success(fav);

    }

    /**
     * 批量删除收藏
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除收藏", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除收藏,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<MemberFavorites> del(@PathVariable String ids) {

        memberFavoritesService.deleteByIds(ids.split(","));
        return CommResult.success("取消收藏");
    }
}
